
import checkPermission from '@/utils/permission'

export const VxeTableMixin = {
  data() {
    return {
      currentRoutePath: this.$route.fullPath,
      routerOk: false,
      tableLoading: false,
      tableTotalRow: 0,
      tableSelectionRows: [],
      tableData: [],
      queryFormRules: {},
      query: {
        type: 'base',
        baseParams: {},
        superParams: [],
        sort: 'id desc',
        page: {
          currentPage: 1,
          pageSize: 10
        }
      },
      api: {
        query: null,
        create: null,
        update: null,
        delete: null
      },
      dialogs: {
        create: {
          visible: false,
          title: '新增',
          formData: {}
        },
        update: {
          visible: false,
          title: '修改',
          formData: {}
        },
        query: {
          form: [],
          formData: {
            tableData: []
          },
          visible: false
        }
      }

    }
  },
  computed: {
    isMobile() {
      return this.$store.state.app.device === 'mobile'
    },
    baseQueryClass() {
      return this.query.type === 'base' ? 'primary' : ''
    },
    superQueryClass() {
      return this.query.type === 'super' ? 'primary' : ''
    }
  },
  watch: {
    $route(to, from) {
      if (this.routerOk && this.currentRoutePath === to.fullPath) {
        this.loadTable()
      } else {
        this.routerOk = true
      }
    }
  },
  mounted() {

  },
  methods: {
    doCheckPermission(value) {
      return checkPermission(value)
    },
    changePage(page) {
      this.query.page = page
      this.loadTable()
    },
    async loadTable() {
      if (!this.api.query) {
        this.$message({ type: 'error', message: '请配置查询方法 Api.query' })
        return
      }
      const $queryForm = this.$refs.queryForm
      if ($queryForm) {
        const formErrorMap = await $queryForm.validate()
        if (formErrorMap) {
          return false
        }
      }
      const queryParams = {}
      if (this.query.type === 'base') {
        const baseParams = {}
        for (const key in this.query.baseParams) {
          if (this.query.baseParams[key] !== undefined && this.query.baseParams[key] !== null) {
            baseParams[key] = this.query.baseParams[key]
          }
        }
        queryParams.baseParams = baseParams
      } else {
        queryParams.superParams = this.query.superParams
      }
      queryParams.page = this.query.page
      queryParams.sort = this.query.sort
      this.loadTableData(queryParams)
    },
    // 基础查询
    async confirmBaseQueryData() {
      const $queryForm = this.$refs.queryForm
      if ($queryForm) {
        const formErrorMap = await $queryForm.validate()
        if (formErrorMap) {
          return false
        }
      }

      this.query.type = 'base'
      this.query.page.currentPage = 1
      this.loadTable()
    },
    // 高级搜索
    confirmSuperQueryData(q) {
      this.query.superParams = q
      this.query.page.currentPage = 1
      this.query.type = 'super'
      this.loadTable()
    },
    // 获取后端数据
    loadTableData(data) {
      this.tableLoading = true
      this.tableSelectionRows = []
      this.api.query(data).then(({ data, rows }) => {
        this.tableData = data
        this.tableTotalRow = Number(rows)
        setTimeout(() => {
          this.tableLoading = false
        }, 200)
      }).catch(() => {
        this.tableLoading = false
      })
    },
    restBaseForm() {
      this.$refs['queryForm'].reset()
      this.confirmBaseQueryData()
    },
    showModal(key, formData) {
      this.dialogs[key].visible = true
      this.dialogs[key].formData = formData
    },
    deleteTableRow(id) {
      if (!this.api.delete) {
        this.$message({ type: 'error', message: '请配置删除方法 Api.delete' })
        return
      }
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.api.delete(id).then(({ code, msg }) => {
          if (code === 200200) {
            this.loadTable()
          }
          this.$message({ type: code === 200200 ? 'success' : 'error', message: msg || '删除成功' })
        })
      }).catch(() => {
      })
    },
    tableSortChange({ property, order }) {
      if (order) {
        this.query.sort = order && property ? (property + ' ' + order) : null
        this.loadTable()
      }
    },
    tableCheckboxChange(tableRef) {
      const $table = this.$refs[tableRef]
      if ($table) {
        this.tableSelectionRows = $table.getCheckboxRecords()
      }
    },
    showSuperQueryModal() {
      this.dialogs.query.visible = true
    },
    confirm({ api, params, title }) {
      this.$confirm((title || '确定执行此操作吗?'), '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        api(params).then(({ code, msg }) => {
          if (code === 200200) {
            this.loadTable()
          }
          this.$message({ type: code === 200200 ? 'success' : 'error', message: msg || '操作成功' })
        })
      }).catch(() => {
      })
    },
    showUploadDialog(url) {
      const $dialog = this.$refs.excelImport
      if ($dialog) {
        $dialog.open(url)
      } else {
        this.$message({ type: 'error', message: '未配置导入模板' })
      }
    }
  }
}

